package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.util.DateUtils;
import cn.e4j.modular.crm.entity.GwfdT;
import cn.e4j.modular.crm.entity.MmrwT;
import cn.e4j.modular.crm.service.impl.GwfdTServiceImpl;
import cn.e4j.modular.crm.service.impl.MmrwTServiceImpl;
import cn.e4j.modular.erp.controller.ErpLogic;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

/**
 * 实际费用结算维护
 * Created by miaoyl on 2019-08-30
 */
@Controller
@RequestMapping("/crm/gwft300")
public class Gwft300Controller extends BaseController {

    @Autowired
    private GwfdTServiceImpl gwfdTService;

    /**
     * 跳转实际费用结算维护管理页面
     */
    @RequiresPermissions("crm:gwft300:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwft300.html";
    }

    /**
     * 查询维修费用结算信息列表
     */
    @RequestMapping("/getGwfdList")
    @RequiresPermissions("crm:gwft300:init")
    @ResponseBody
    public ResponseData getListData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwfdTService.getGwfdList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 新增
     */
    @RequestMapping("/addData")
    @RequiresPermissions("crm:gwft300:add")
    @ResponseBody
    public ResponseData add(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwfdT> gwfdTList = inDto.getReqList(GwfdT.class);
        gwfdTService.addData(gwfdTList);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/editData")
    @RequiresPermissions("crm:gwft300:edit")
    @ResponseBody
    public ResponseData editData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwfdT> gwfdTList = inDto.getReqList(GwfdT.class);
        gwfdTService.editData(gwfdTList);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwft300:del")
    @ResponseBody
    public ResponseData del(GwfdT gwfdT) {
        gwfdTService.delete(gwfdT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/deleteDataBatch")
    @RequiresPermissions("crm:gwft300:del")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwfdT> gwfdTList = inDto.getReqList(GwfdT.class);
        gwfdTService.delDataBatch(gwfdTList);
        return SUCCESS_TIP;
    }

    /**
     * 修改状态
     */
    @RequestMapping("/updateStatus")
    @RequiresPermissions("crm:gwft300:init")
    @ResponseBody
    public ResponseData updateStatus(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwfdT> gwfdTList = inDto.getReqList(GwfdT.class);
        gwfdTService.updateStatus(gwfdTList);
        return SUCCESS_TIP;
    }

    /**
     * 新增数据
     */
    @RequestMapping("/getAddData")
    @ResponseBody
    public ResponseData getAddData(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        outDto.put("GWFDDOCNO", "FYJS" + DateUtils.getDate("yyyyMMddHHmmssSSS"));
        return ResponseData.success(outDto);
    }

    /**
     * 获取汇率
     */
    @RequestMapping("/getHuiLv")
    @ResponseBody
    public ResponseData getHuiLv(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto result = gwfdTService.getHuiLv(inDto);
        return ResponseData.success(result);
    }

    /**
     * 根据编号获取信息
     */
    @RequestMapping("/getGwfdData")
    @ResponseBody
    public ResponseData getGwfdData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwfdTService.getGwfdData(inDto);
        return ResponseData.success(result);
    }


    @RequestMapping("/getCount")
    @ResponseBody
    public ResponseData getCount(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto result = gwfdTService.getCount(inDto);
        return ResponseData.success(result);
    }

    /**
     * 根据编号获取信息
     */
    @RequestMapping("/autoGeneration")
    @RequiresPermissions("crm:gwft300:batch")
    @ResponseBody
    public ResponseData autoGeneration(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = Dtos.newDto();
        try {
            outDto = gwfdTService.autoGeneration(inDto);
        }catch (Exception e){
            e.printStackTrace();
            outDto.put("message", "覆盖失败！");
            return ResponseData.success(outDto);
        }
        return ResponseData.success(outDto);
    }
}
